/*
 * jQuery File Upload Plugin JS Example 5.0.2
 * https://github.com/blueimp/jQuery-File-Upload
 *
 * Copyright 2010, Sebastian Tschan
 * https://blueimp.net
 *
 * Licensed under the MIT license:
 * http://creativecommons.org/licenses/MIT/
 */

/*jslint nomen: true */
/*global $ */
/*
$(function () {
    'use strict';

    // Initialize the jQuery File Upload widget:
    $('#fileupload').fileupload();

    // Load existing files:
    $.getJSON($('#fileupload form').prop('action'), function (files) {
        var fu = $('#fileupload').data('fileupload');
        fu._adjustMaxNumberOfFiles(-files.length);
        fu._renderDownload(files)
            .appendTo($('#fileupload .files'))
            .fadeIn(function () {
                // Fix for IE7 and lower:
                $(this).show();
            });
    });

    // Open download dialogs via iframes,
    // to prevent aborting current uploads:
    $('#fileupload .files a:not([target^=_blank])').live('click', function (e) {
        e.preventDefault();
        $('<iframe style="display:none;"></iframe>')
            .prop('src', this.href)
            .appendTo('body');
    });

});
*/

$(function () {
    'use strict';

    $.widget('breadtrip.fileupload', $.blueimpUI.fileupload, {
        options: {
            numberOfWaitingFiles: 0,

            add: function(e, data) {
                var that = $(this).data('fileupload');
                $("#fileupload .fileupload-form").hide();
                $("#fileupload .fileupload-content").show();
                that.options.numberOfWaitingFiles += data.files.length;
                that._renderHeader();
                $.blueimpUI.fileupload.prototype.options.add.call(this, e, data);
            },

            send: function (e, data) {
                data.context.find('.progress').show();
                $.blueimpUI.fileupload.prototype.options.send.call(this, e, data);
            },

            done: function (e, data) {
                var that = $(this).data('fileupload');
                that.options.numberOfWaitingFiles -= 1;
                that._renderHeader();
                $.blueimpUI.fileupload.prototype.options.done.call(this, e, data);
                if(that.options.numberOfWaitingFiles == 0) {
                    $(this).find('.selecting').hide();
                    $(this).find('.uploading').hide();
                    $(this).find('.finishing').show();
                }

            },

            fail: function (e, data) {
                var that = $(this).data('fileupload');
                that.options.numberOfWaitingFiles -= data.files.length;
                that._renderHeader();
                if(data.errorThrown === "abort") {
                    $(this).find('.selecting').show();
                    $(this).find('.uploading').hide();
                }
                $.blueimpUI.fileupload.prototype.options.fail.call(this, e, data);
            }
        },

        _renderHeader: function() {
            $("#fileupload .info .waiting-num").html(this.options.numberOfWaitingFiles);
        },

        _initFileUploadButtonBar: function () {
            var fileUploadButtonBar = this.element.find('.fileupload-buttonbar'),
                filesList = this.element.find('.files'),
                ns = this.options.namespace;

            fileUploadButtonBar.find('.start')
                .bind('click.' + ns, function (e) {
                    e.preventDefault();
                    fileUploadButtonBar.find('.selecting').hide();
                    fileUploadButtonBar.find('.uploading').show();
                    filesList.find('.start button').click();
                });
            fileUploadButtonBar.find('.cancel')
                .bind('click.' + ns, function (e) {
                    e.preventDefault();
                    filesList.find('.cancel button').click();
                });
            fileUploadButtonBar.find('.delete')
                .bind('click.' + ns, function (e) {
                    e.preventDefault();
                    filesList.find('.delete button').click();
                });
        },

        _renderUpload: function (files) {
            var that = this,
                options = this.options,
                tmpl = this._renderUploadTemplate(files);
            if (!(tmpl instanceof $)) {
                return $();
            }
            tmpl.css('display', 'none');
            // .slice(1).remove().end().first() removes all but the first
            // element and selects only the first for the jQuery collection:
            tmpl.find('.progress div').slice(1).remove().end().first()
                .progressbar();
            tmpl.find('.start button').slice(
                this.options.autoUpload ? 0 : 1
            ).remove().end().first()
                .button({
                    text: false
                });
            tmpl.find('.cancel button').slice(1).remove().end().first()
                .button({
                    text: false
                });
            tmpl.find('.preview').each(function (index, node) {
                that._loadImage(
                    files[index],
                    function (img) {
                        $(img).hide().appendTo(node).fadeIn();
                    },
                    {
                        maxWidth: options.previewMaxWidth,
                        maxHeight: options.previewMaxHeight,
                        fileTypes: options.previewFileTypes,
                        canvas: options.previewAsCanvas
                    }
                );
            });
            return tmpl;
        },

        _renderDownload: function (files) {
            var tmpl = this._renderDownloadTemplate(files);
            if (!(tmpl instanceof $)) {
                return $();
            }
            tmpl.css('display', 'none');
            tmpl.find('.delete button').button({
                text: false
            });
            tmpl.find('a').each(this._enableDragToDesktop);
            return tmpl;
        }
    });
    // Initialize the jQuery File Upload widget:
    $('#fileupload').fileupload();

    // Load existing files:
    $.getJSON($('#fileupload form').prop('action'), function (files) {
        var fu = $('#fileupload').data('fileupload');
        fu._adjustMaxNumberOfFiles(-files.length);
        fu._renderDownload(files)
            .appendTo($('#fileupload .files'))
            .fadeIn(function () {
                // Fix for IE7 and lower:
                $(this).show();
            });
    });
});

